<?php

namespace App\Recharge\Api;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Gongjiangrenxin extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'Merchant_number' => $bean->getSupplierNum(), //商户密钥
            'account' => $bean->getAccount(),
            'gameAcct' => null,
            'gameAreaId' => null,
            'gameAreaName' => null,
            'gameServerId' => null,
            'gameServerName' => null,
            'goodsId' => $bean->getGoodsId(), //商品编码
            'number' => (string)($bean->getRechargeNum() * $bean->getStartCharging()),
            'order' => $bean->getOrderNo(), //订单号
            'price' => $bean->getUnitPrice(),
            'token_url' => $bean->getNotifyUrl(), //异步回调地址
            'ip' => $bean->getIp(),//获取用户当前的ip,
        ];
        ksort($array);
        $array['sign'] = md5(json_encode($array) . $bean->getSecretKey());
        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('status', '');
        $order_sn = $request->input('merchantOrder', '');
        return $this
            ->apiNotifyData($order_sn, $status)
            ->setExecute(in_array($status, ['1', '2']))
            ->setRechargeSuccess($status == '1')
            ->setRechargeErrorMessage($request->input('mes', ''));
    }
}
